import * as pdfjsLib from 'pdfjs-dist';
import 'pdfjs-dist/build/pdf.worker';


// Setting worker path to worker bundle.
pdfjsLib.GlobalWorkerOptions.workerSrc =
    "pdf.worker.bundle.js";

const upForm: HTMLFormElement = document.querySelector("#upForm")
const file: HTMLInputElement = document.querySelector("#file")

upForm.addEventListener("submit", function (e) {
    e.preventDefault();
    const upFile = file.files[0];
    const reader = new FileReader();


    // 监听 FileReader 的 load 事件
    reader.addEventListener("load", () => {
        const fileContent = reader.result as string;
        console.log("文件内容：", fileContent);
    });
    //reader.readAsText(upFile);
    return false
})


const pdfPath = "http://localhost:8080/helloworld.pdf";

console.log("=====  pdf: ", pdfPath)

// Loading a document.
const loadingTask = pdfjsLib.getDocument(pdfPath);


loadingTask.promise.then(async function (pdfDocument) {
    // Request a first page
    console.log("=======: ", pdfDocument);
    return pdfDocument.getPage(1).then(function (pdfPage) {
        // Display page on the existing canvas with 100% scale.
        const viewport = pdfPage.getViewport({ scale: 1.0 });
        const canvas : HTMLCanvasElement = <HTMLCanvasElement>document.getElementById("theCanvas");
        canvas.width = viewport.width;
        canvas.height = viewport.height;
        const ctx = canvas.getContext("2d");
        const renderTask = pdfPage.render({
            canvasContext: ctx,
            viewport,
        });
        return renderTask.promise;
    }).finally(function () {
        // Update UI elements
        console.log("finally ====")
    });
}).catch(function (reason) {
    console.error("Error:---- " + reason);
}).finally(function () {
    console.log("finally ====")
}).then(function () {
    console.log("then ====")
});